<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tool :node-tree="company" :is-root="true" @addDeparts="addDeparts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tool slot-scope="{data}" :node-tree="data" @editDeparts="editDeparts" @addDeparts="addDeparts" @delDeparts="getDepartments" />
        </el-tree>
      </el-card>
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDeparts="getDepartments" />
    </div>
  </div>
</template>

<script>
import TreeTool from './components/tree-tool.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
export default {
  components: {
    TreeTool,
    AddDept
  },
  data() {
    return {
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      departs: [{ name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕' }] },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      showDialog: false,
      node: null,
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      // this.departs = result.depts
      this.departs = transListToTreeData(result.depts, '')
      this.loading = false
    },
    addDeparts(node) {
      this.showDialog = true
      this.node = node
    },
    editDeparts(node) {
      this.showDialog = true
      this.node = node
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scope >
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
